<?
function drizzo_login() {
	global $drizzo_users, $_POST;

	require_once('config.php');
	require_once('functions_db.php');	
	drizzo_db_connect();
	
	if ($_POST['login'] == "login") {
		$username = $_POST['username'];
		$password = $_POST['password'];
		$remember = $_POST['remember'];
		$password = md5($password);

		$result = @mysql_query("SELECT `username`,`password`,`id` FROM `$drizzo_users` WHERE `username`='$username'");

		if (!$result) {		
			echo("<p>Error performing query: " . mysql_error() . "</p>");
		} else {
			while ($row = mysql_fetch_array($result)) {
				$db_user = $row['username'];
				$db_pass = $row['password'];
				$db_id = $row['ID'];
				
				if (($db_user == $username) && ($db_pass == $password)) {
					$db_user = md5($row['username']);
					$db_pass = md5($row['password']);

					if(!$remember) {
						setcookie('drizzo_user', $db_user, null, '/');
						setcookie('drizzo_pass', $db_pass, null, '/');
						header("Location: http://" . domain_name . admin_folder . "");
					} else {
						$cookie_expire = time()+(365*24*60*60);
						setcookie('drizzo_user', $db_user, $cookie_expire, '/');
						setcookie('drizzo_pass', $db_pass, $cookie_expire, '/');
						header("Location: http://" . domain_name . admin_folder . "");
					}

				} else {
				}
			}
		}
	}
	

	if ($_POST['login'] == "logout") {
		setcookie('drizzo_user', '', time() - 3600, '/');
		setcookie('drizzo_pass', '', time() - 3600, '/');
		header("Location: http://" . domain_name . admin_folder . "");
		exit();
	}
}

function drizzo_login_check() {
	if(!empty($_COOKIE['drizzo_user']) && !empty($_COOKIE['drizzo_pass'])) {
		return true;
	} else {
		return false;
	}
}

function drizzo_protector() {
	if(drizzo_login_check()) {
		return true;
	} else {
		include_once("http://" . domain_name . admin_folder . "/engine/login_inc.php");
		include_once("http://" . domain_name . admin_folder . "/footer.php");
		exit();
	}
}

?>